#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""  
@Project : pythonProject01
@File : mysql_val.py
@Author : erhu
@Time : 2025/9/9 18:48  
@脚本说明 : 
"""

from .ccolor import Colors
import ipaddress
import threading
import pymysql
def scan_mysql(host="127.0.0.1", port=3306, user="root", password="", connect_timeout=5):
    try:
        pymysql.connect(
            host=host,
            port=port,
            user=user,
            password=password,
            connect_timeout=connect_timeout,
            database="information_schema",
            charset="utf8",
            autocommit=True,
        )
        print(f"[+] {host}:{port} 存在MySQL数据库弱口令 -> {Colors.RED_BRIGHT}{Colors.BOLD}{user} / {password}{Colors.RESET}")
    except:
        pass
def mysql_run(host, port, users, passwds):
    try:
        threads = []
        print("======MySQL弱口令漏扫======")
        passwords = [p.strip() for p in passwds]
        for ip in ipaddress.IPv4Network(host):
            for user in users:
                # 读取密码字典
                for passwd in passwords:
                    # 弱口令扫描 注意需要去除空白符
                    t = threading.Thread(target=scan_mysql, args=(str(ip), port, user.strip(), passwd.strip()))
                    t.start()
                    threads.append(t)
            for t in threads:
                t.join()
    except:
        pass